﻿Imports System.Windows
Imports CommunityToolkit.Mvvm.ComponentModel
Imports CommunityToolkit.Mvvm.Input

Public Class SignUpPageViewModel : Inherits ObservableObject

    Private _username As String
    Public Property Username As String
        Get
            Return _username
        End Get
        Set(value As String)
            SetProperty(_username, value)
        End Set
    End Property

    Private _password As String
    Public Property Password As String
        Get
            Return _password
        End Get
        Set(value As String)
            SetProperty(_password, value)
        End Set
    End Property

    Private _confirmPassword As String
    Public Property ConfirmPassword As String
        Get
            Return _confirmPassword
        End Get
        Set(value As String)
            SetProperty(_confirmPassword, value)
        End Set
    End Property

    Private _phone As String
    Public Property Phone As String
        Get
            Return _phone
        End Get
        Set(value As String)
            SetProperty(_phone, value)
        End Set
    End Property

    Private _errorMessage As String
    Public Property ErrorMessage As String
        Get
            Return _errorMessage
        End Get
        Set(value As String)
            SetProperty(_errorMessage, value)
        End Set
    End Property

    Private ReadOnly _client As New HttpClientBase()

    Public Property SignUpCommand As New AsyncRelayCommand(AddressOf SignUpClicked)

    Private Async Function SignUpClicked() As Task
        '判断 username 不为空，长度大于 2，小于 10，且不包含空格
        If String.IsNullOrEmpty(Username) OrElse Username.Length < 3 OrElse Username.Length > 10 OrElse Username.Contains(" ") Then
            ErrorMessage = "用户名不能为空，长度必须在 4 到 9 个字符之间，且不能包含空格"
            Return
        End If
        '判断 password 不为空，长度大于 6，小于 16，且不包含空格
        If String.IsNullOrEmpty(Password) OrElse Password.Length < 7 OrElse Password.Length > 16 OrElse Password.Contains(" ") Then
            ErrorMessage = "密码不能为空，长度必须在 8 到 15 个字符之间，且不包含空格"
            Return
        End If
        '判断 confirmPassword 与 password 相同
        If Password <> ConfirmPassword Then
            ErrorMessage = "两次输入的密码不一致"
            Return
        End If
        '判断 phone 不为空，长度为 11，且为数字
        If String.IsNullOrEmpty(Phone) OrElse Phone.Length <> 11 OrElse Not Phone.All(Function(c) Char.IsDigit(c)) Then
            ErrorMessage = "手机号码不能为空，长度必须为 11 位数字"
            Return
        End If
        '调用服务端 API 注册用户
        Dim response = Await _client.PostAsync(Of User)(
            New User() With {
                .Username = Username,
                .Password = Password,
                .Phone = Phone,
                .Avatar = Nothing
            }, "vdm/api/account/signup")
        If response.Code = 200 Then
            '注册成功
            ' 跳转到登录页面
            MessageBox.Show("注册成功，用户 ID 为" + response.Data.Id + vbCrLf + "请牢记用户 ID 和密码，它们将作为登录平台的唯一凭据")
            NavigationService.Navigate(New Uri("/VMDWebFrontend;component/View/LoginPage.xaml", UriKind.Relative))
        Else
            '注册失败
            ErrorMessage = "注册失败：" + response.Message
        End If
    End Function

End Class
